﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Net.Mail;
using System.Net;

public partial class RequestList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //post back
        if (!Page.IsPostBack)
        {
            if (Session["user"] != null)
            {
                this.Log_in1.Visible = false;
                this.Log_out1.Visible = true;
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
            LoadGridView();
        }
        else
        {

        }
    }

    //Load request for grid view
    private void LoadGridView()
    {
        ProRequest pr = new ProRequest();
        int intStatus = 4;//pending
        DataSet ds = new DataSet();

        //get params
        if (Request.Params.Get("rql") != null && Session["user"] != null)
        {
            ds = pr.LoadRequestInfoToConfirm(Session["user"].ToString(), intStatus);
            this.grdRequestList.DataSource = ds.Tables[0];
            this.grdRequestList.DataBind();
        }
    }

    protected void grdRequestList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //set index row
        this.grdRequestList.SelectedIndex = Int32.Parse(e.CommandArgument.ToString());
    }
    protected void grdRequestList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //accept
        ProRequest pr = new ProRequest();
        int id = Int32.Parse(this.grdRequestList.SelectedRow.Cells[14].Text.ToString());
        String strRequestCode = this.grdRequestList.SelectedRow.Cells[3].Text.ToString();
        pr.UpdateRequestConfrim(Constant.REQUEST_STATUS_ACCEPT, strRequestCode, id);

        DataSet ds1 = new DataSet();
        DataSet ds2 = new DataSet();
        DataSet ds3 = new DataSet();
        DataSet ds4 = new DataSet();
        ds1 = pr.LoadRequestInfo(strRequestCode);

        ds2 = new DataSet();
        ds2 = pr.LoadPic(ds1.Tables[0].Rows[0]["pic"].ToString());

        ds3 = new DataSet();
        ds3 = pr.LoadDirector(ds1.Tables[0].Rows[0]["director"].ToString());

        ds4 = new DataSet();
        ds4 = pr.LoadVPre(ds1.Tables[0].Rows[0]["vpresident"].ToString());

        //Update status for next request confirm depend on confirm by.
        //sent email to next manager
        DataSet ds = pr.LoadRequestInfo(strRequestCode);
        if (Session["user"].ToString() == ds.Tables[0].Rows[0]["pic"].ToString())
        {
            //PIC is processing Request Form
            pr.UpdateRequestConfrim(Constant.REQUEST_STATUS_PENDING, strRequestCode, id);
            SentToManager(ds3.Tables[0].Rows[0]["email"].ToString(), ds3.Tables[0].Rows[0]["username"].ToString(), 
                Session["user"].ToString(), "Request From need to process", Request.Params.Get("rqid"));
        }
        else if (Session["user"].ToString() == ds.Tables[0].Rows[0]["director"].ToString())
        {
            //Director is processing Request Form
            pr.UpdateRequestConfrim(Constant.REQUEST_STATUS_PENDING, strRequestCode, id);
            SentToManager(ds4.Tables[0].Rows[0]["email"].ToString(), ds4.Tables[0].Rows[0]["username"].ToString(),
                Session["user"].ToString(), "Request From need to process", Request.Params.Get("rqid"));
        }
        else
        {
            //VPre is processing Request Form
            pr.UpdateRequestInfoStatus(strRequestCode, Constant.REQUEST_STATUS_DONE);
            SentToSubmitter(ds1.Tables[0].Rows[0]["username"].ToString(), "Accept Request", strRequestCode);
        }
    }

    protected void grdRequestList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //reject
        ProRequest pr = new ProRequest();
        String strRequestCode = this.grdRequestList.SelectedRow.Cells[3].Text;
        pr.UpdateRequestConfrim(Constant.REQUEST_STATUS_REJECT, strRequestCode, Int32.Parse(this.grdRequestList.SelectedRow.Cells[14].ToString()));
        pr.UpdateRequestInfoStatus(strRequestCode, Constant.REQUEST_STATUS_REJECT);

        DataSet ds1 = new DataSet();
        ds1 = pr.LoadRequestInfo(strRequestCode);
        SentToSubmitter(ds1.Tables[0].Rows[0]["username"].ToString(), "Reject Request", strRequestCode);
    }

    public void SentToSubmitter(String strToMail, String strSubject, String strRqid)
    {
        MailMessage mm = new MailMessage();
        mm.From = new MailAddress(Constant.EMAIL_ADMIN, Constant.EMAIL_DISPLAY_ADMIN);
        mm.To.Add(new MailAddress(strToMail));
        mm.Subject = strSubject;
        mm.IsBodyHtml = true;
        mm.Body = "<div  style=\"width:600px; background-color:#fff; margin:0 auto; float:left; margin-left:10px;text-align:justify;\">" +
        "<img src=\"images/logo copy.jpg\" style=\"margin:10px 0;\" /><br />" +
        "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">Hi <strong>" + Session["user"].ToString() + "</strong>!</p>" +
        "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">Your request have been Accept</p>" +
        "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">Please click below link to review or edit your request:</p>" +
        "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\"><a href=\"http://techdata.com.vn/requestform.aspx?rqid=" + strRqid + "\">http://techdata.com.vn/requestform.aspx?rqid=" + strRqid + "</a></p>" +
        "<p>-----------------------------------------------------------------------</p>" +
        "<p>If you have more requires, please contact Techdata Support Team to get more information.</p>" +
        "<p><span class=\"style1\">Techdata Administrator Team</span><br />" +
        "Email: <a href=\"mailto:info@techdata.com.vn\">info@techdata.com.vn</a><br />" +
        "Phone: 0907246145<br />" +
        "  Website: <a href=\"http://techdata.com.vn\">techdata.com.vn</a></p>" +
        "<p>      Thanks for use our service!</p>" +
        "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">&nbsp;</p>" +
        "<p style=\"width:600px; text-align:justify;margin:0px; float:left;\">&nbsp;</p>" +
        "<br />" +
        "</div>";

        SmtpClient sc = new SmtpClient();
        sc.Host = "smtp.gmail.com";
        sc.Port = 587;
        sc.Credentials = new NetworkCredential(Constant.EMAIL_ADMIN, Constant.PASS_EMAIL_ADMIN);
        sc.EnableSsl = true;
        sc.DeliveryMethod = SmtpDeliveryMethod.Network;
        sc.Send(mm);
    }
    public void SentToManager(String strMailTo, String strPIC, String strSubmitter, String strSubject, String strRqid)
    {
        MailMessage mm = new MailMessage();
        mm.From = new MailAddress(Constant.EMAIL_ADMIN, Constant.EMAIL_DISPLAY_ADMIN);
        mm.To.Add(new MailAddress(strMailTo));
        mm.Subject = strSubject;
        mm.IsBodyHtml = true;
        mm.Body =
      "<div  style=\"width:600px; background-color:#fff; margin:0 auto; float:left; margin-left:10px;text-align:justify;\">" +
      "<img src=\"images/logo copy.jpg\" style=\"margin:10px 0;\" /><br />" +
      "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">Hi <strong>" + strPIC + "</strong>!</p>" +
      "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">You have <strong>request</strong> from <strong>" + strSubmitter + "<strong>.</p>" +
      "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">Please click below link to view all request waitting to review and process:</p>" +
      "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\"><a href=\"http://techdata.com.vn/requestlist.aspx?cfb=" + strPIC + "\">http://techdata.com.vn/requestlist.aspx?cfb=" + strPIC + "</a></p>" +
      "<p>-----------------------------------------------------------------------</p>" +
      "<p>If you have more requires, please contact Techdata Support Team to get more information.</p>" +
      "<p><span class=\"style1\">Techdata Administrator Team</span><br />" +
      "Email: <a href=\"mailto:info@techdata.com.vn\">info@techdata.com.vn<</a><br />" +
      "Phone: 0907246145<br />" +
      "  Website: <a href=\"http://techdata.com.vn\">techdata.com.vn</a></p>" +
      "<p>      Thanks for use our service!</p>" +
      "<p style=\"width:600px; text-align:justify;margin:0px; margin-top:20px;float:left;\">&nbsp;</p>" +
      "<p style=\"width:600px; text-align:justify;margin:0px; float:left;\">&nbsp;</p>" +
      "<br />" +
      "</div>";

        SmtpClient sc = new SmtpClient();
        sc.Host = "smtp.gmail.com";
        sc.Port = 587;
        sc.Credentials = new NetworkCredential(Constant.EMAIL_ADMIN, Constant.PASS_EMAIL_ADMIN);
        sc.EnableSsl = true;
        sc.DeliveryMethod = SmtpDeliveryMethod.Network;
        sc.Send(mm);
    }
}
